<?php

class MM_Controller_Action extends Zend_Controller_Action {
	
	/**
	 * Current user object
	 * @var User
	 */
	protected $me;
	
	/**
	 * Translation object
	 * @var MM_Translate
	 */
	protected $translate;
	
	/**
	 * Default session namespace
	 * @var Zend_Session_Namespace
	 */
	protected $_session;
	
	function init() {
		//me!
		$this->me = Zend_Registry::get('User');
		
		$this->_session = new Zend_Session_Namespace('controller_' . get_class($this));
		
		$lngnsp = new Zend_Session_Namespace('lang');
		
		if(isset($lngnsp->lang))
			$this->translate->setLocale($lngnsp->lang);
		else if($this->me->locale)
			$this->translate->setLocale($this->me->locale);
		
		$view = new MM_View();
		$view->translate = $this->translate;
		
		foreach ($this->view->getScriptPaths() as $path)
			$view->addScriptPath($path);
		
			
		foreach ($this->view->getHelperPaths() as $path)
			$view->addHelperPath($path);
			
		$this->view = $view;
		$this->view->baseURL = Zend_Registry::get('global')->url;
		$this->view->me = $this->me;	
		// custom viewrenderer
		
		Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
		
		$vr = new MM_ViewRenderer($view);
		$vr->setActionController($this);
			
		Zend_Controller_Action_HelperBroker::addHelper($vr);
		
		// custom helpers
		Zend_Controller_Action_HelperBroker::addPrefix('MM_Controller_Action_Helper');
		
		//takie tam! 

		
		if($this->me->isSuperAdmin())
			$this->view->onlineUsers = User::getOnlineUsers();
									
									
		
		if($c = $this->me->getReceivedMessagesCount(true)) {
			$this->view->warnMessage = $this->_("Masz nową wiadomość!");			
		}

		if( $this->me->isClient() 
					&& 
			!$this->me->bylaw 
					&& 
			!($this->_getParam('controller') == 'index' 
					|| 
			$this->_getParam('controller') == 'profile')) {
				
			$this->_redirect('/index/bylaw/');
				
//			$reg = @file_get_contents(realpath(dirname(__FILE__). '/../../../../'. Zend_Registry::get('auth')->bylawFile));
//			throw new MM_Exception_Fail("<div class=\"bylaw\">$reg</div>", array(
//				array('Akceptuję', array('controller' => 'profile', 'action' => 'bylaw')),
//				array('Nie akpetpuję', array('controller' => 'profile', 'action' => 'logout')),
//			), "Regulamin");
		}					
		
	}

    public function _($messageId, $locale = null)
    {
        return $this->translate->translate($messageId, $locale);
    }
    
}
